import 'package:any_reader/model/article.dart';
import 'package:any_reader/public.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ReaderScrollView extends StatelessWidget {
  final Article article;
  final double fontSize;
  final double lineHeight;
  final Color textColor;
  final double padding;
  final double readeAreaHeight;
  final String chapterTitle;
  ReaderScrollView({
    this.article,
    this.fontSize,
    this.lineHeight,
    this.textColor,
    this.padding,
    this.chapterTitle,
    this.readeAreaHeight,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(
        padding,
        ScreenUtil().setHeight(10),
        padding,
        ScreenUtil().setHeight(100),
      ),
      height: article.pageHeight,
      child: Column(
        children: buildContent(article),
      ),
    );
  }

  List<Widget> buildContent(Article article) {
    List<Widget> _list = [];
    for (int index = 0; index < article.pageOffsets.length; index++) {
      var _item = article.pageOffsets[index]['page'];
      PagePainter _pagePainter = PagePainter(item: _item);
      _list.add(Center(
        child: Container(
          child: Container(
            color: Colors.purple,
            width: ScreenUtil().setWidth(750) - padding * 2,
            child: CustomPaint(
              painter: _pagePainter,
            ),
          ),
        ),
      ));
    }
    return _list;
  }
}

class PagePainter extends CustomPainter {
  List<List<Map<String, dynamic>>> item;
  PagePainter({
    this.item,
  });

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    for (int j = 0; j < item.length; j++) {
      for (int k = 0; k < item[j].length; k++) {
        Map<String, dynamic> _item = item[j][k];
        canvas.drawParagraph(_item["paragraph"], _item["offset"]);
      }
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
    throw UnimplementedError();
  }
}

//Text.rich(
//TextSpan(
//style: TextStyle(
//fontSize: fontSize * 0.98,
//color: textColor,
//height: lineHeight * 0.98,
//letterSpacing: 0,
//wordSpacing: 0,
//fontFamily: "dengxian",
//textBaseline: TextBaseline.ideographic,
//),
//text: content,
//),
//textAlign: TextAlign.justify,
//softWrap: true,
//textWidthBasis: TextWidthBasis.longestLine,
//textDirection: TextDirection.ltr,
//)
